-- 游客状态
_G['OPCODE'] = {

-- 心跳包
	[AEvent.HEART] = function(socket,dataEx)
	        io.write('receive heart------------------------------\n')
			return 0
	end,
	-- 验证身份
	[AEvent.VALIDATE_ID] = function(socket,dataEx)

		local arg = dataEx:split(_1_);
		if (arg[2] == nil or arg[1] ==nil)then
			io.write("---------------------xxx1------------------\n");
			CNetSend(socket,AEvent.ERROR_STATE);
	 		return 1;
		end

		local arg2 = arg[1]:split(_2_);
		local keyTime = os.date("%Y-%m-%d",os.time());

		if(arg2[1] == nil or arg2[2] == nil or arg2[3] == nil or arg2[4] == nil or arg2[5] == nil or arg2[6] == nil or arg2[7] == nil or arg2[8] == nil or arg2[9] == nil or arg2[10] == nil) then
			io.write("---------------------xxx2------------------\n");
			CNetSend(socket,AEvent.ERROR_STATE);
			return 1;
		end


		local key = CMd5(arg2[1] .. systemKey .. keyTime);
		--local key1 = CMd5('123');
		--print(arg2[1] .. systemKey .. keyTime)
		--print(key)
		-- 检验明文
		if key ~= arg[2] then
		    print('ERR_MSG')
			CNetSend(socket,AEvent.ERROR_MSG);
			return 1;
		end

		-- 如果重复加入
		if argUser[arg2[2]] then
			--发送给之前的人
			local oldSocket = argUser[arg2[2]].socket;
			OnClose_Socket(oldSocket);
			--告诉前一个客户端主动断开连接
			CNetSend(oldSocket,AEvent.VALIDATE_ID_FALSE);
			io.write("caught rejoin close the oldsocket id="..oldSocket.."\n");
			--不返回 不然会把当前客户端踢掉
			--return 1;
		end
		local user;

		if not argSocket[socket] then
			user = User:new();

			user.socket = socket;  --socket
			user.id = arg2[1];  --id
			user.name = arg2[2];  --名字
			user.level = arg2[3]; --等级
			user.vip = arg2[4]; --vip等级
			user.honesty = arg2[5]; --阵营
--~ 			user.lock = arg2[6]; --是否被锁
			if arg2[6] == '1' or arg2[6] == 1 then
				--加入黑名单
				if not argUserLock[arg2[2]] then
					argUserLock[arg2[2]] = arg2[2];
				end
			else
				argUserLock[arg2[2]] = nil;
			end
			user.userMod = arg2[7]; --模型
			user.gender = arg2[8]; --男女
			user.leagueName = arg2[9]; --帮派名
			user.title = arg2[10]; --称号
			user.platform = arg2[11]; --平台
			user.teamList = {}; --群组列表
			user.lastChat = os.time()-11;
			argSocket[socket] = user;
			argUser[user.name] = user;
			local userCount = 0;
			for k,v in pairs(argSocket) do
				userCount = userCount + 1;
			end
			io.write("-----------------user count="..userCount.."\n");
		else
			user = argSocket[socket];
		end
		-- 2.发送消息
		CNetSend(user.socket,AEvent.VALIDATE_ID_SUCCESS);

	end,

	--加入群组
	[AEvent.JOIN_TEAM] = function(socket,dataEx)
		if not argSocket[socket] then
			return 1;
		end
		local user = argSocket[socket];

		local arg = dataEx:split(_1_);

		if(arg[1]==nil) then
			io.write('---------------------------------------nil\n');
			return 1;
		end

		--如果有就加入，没有就创建
		if not argTeam[arg[1]] then
			local team = Team:new();
			team.userList = {};
			-- 1.更新属性
			team.name = arg[1];
			if arg[2] ~= nil then
				team.info = arg[2];
			end
			team.userList[user.name] = user;
			argTeam[arg[1]] = team;

		else
			--加入到群组
			 argTeam[arg[1]].userList[user.name] = user;
		end

		--个人信息里增加群组

		argSocket[socket].teamList[arg[1]] = arg[1];

		CNetSend(socket,AEvent.JOIN_TEAM_SUCCESS .._EN_.. arg[1]);


		if string.find(filter,string.sub(arg[1],0,1)) ~= nil and string.find(filter,string.sub(arg[1],0,1)) >= 1 then
			for k,v in pairs(argTeam[arg[1]].userList) do
			    if user.platform ~= nil then
				    CNetSend(v.socket,AEvent.PLAYER_JOIN_TEAM .._EN_.. arg[1] .._1_.. user.name .._3_.. user.id .._3_..  user.level  .._3_..  user.vip .._3_..  user.userMod .._3_..  user.leagueName .._3_..  user.title .._3_.. user.honesty .._3_ .. user.platform);
				else
					CNetSend(v.socket,AEvent.PLAYER_JOIN_TEAM .._EN_.. arg[1] .._1_.. user.name .._3_.. user.id .._3_..  user.level  .._3_..  user.vip .._3_..  user.userMod .._3_..  user.leagueName .._3_..  user.title .._3_.. user.honesty);
				end
			end
		end
	end,

	--退出群组
	[AEvent.EXIT_TEAM] = function(socket,dataEx)
		if not argSocket[socket] then
			io.write('---------------------------------------nil1\n');
			return 1;
		end

		local arg = dataEx:split(_1_);

		if(arg[1]==nil) then
			io.write('---------------------------------------nil2\n');
			return 1;
		end

		--删除掉群组里的用户信息
		local user = argSocket[socket];

		argSocket[socket].teamList[arg[1]] = nil;
		--删除掉用户里的群组信息

		if not argTeam[arg[1]] then
			io.write('---------------------------------------nil3\n');
--~ 			return 1;
		else
			local team = argTeam[arg[1]];
			-- 发送给群组内所有人，有人退出
			local userList = team.userList;


			if string.find(filter,string.sub(arg[1],0,1)) ~= nil and string.find(filter,string.sub(arg[1],0,1)) >= 1 then
				for k,v in pairs(userList) do
				    if v.socket ~= socket then
						CNetSend(v.socket,AEvent.PLAYER_QUIT_TEAM .._EN_.. arg[1] .._1_.. user.name .._3_.. user.id);
					end
				end
			end
			argSocket[socket].teamList[arg[1]] = nil;
			argTeam[arg[1]].userList[user.name] = nil;
			local userCount = 1;
			for k,v in pairs(argTeam[arg[1]].userList) do
				userCount = userCount + 1;
			end

			if userCount <=0 then
				argTeam[arg[1]] = nil;
				io.write('---------------------------------------清理成功'..k..'\n');
			end
		end
	end,
	--解散群组
	[AEvent.DISMISS_TEAM] = function(socket,dataEx)
		if not argSocket[socket] then
			io.write('---------------------------------------nil1\n');
			return 1;
		end

		local arg = dataEx:split(_1_);

		if(arg[1]==nil or arg[2]==nil) then
			io.write('---------------------------------------nil2\n');
			return 1;
		end

		--判断密钥是否正确
		if systemKey ~= arg[2] then
			--输出错误
			if __DEBUG__ then
				io.write('---------------------------------------systemkey error\n');
			end
			return 1;
		end

		--开始解散

		local o =0;
		if string.find(filter,string.sub(k,0,1),0) == 1 then
			--广播到群组里
			for j,m in pairs(argTeam[k].userList) do
				if m.socket ~= socket then
					CNetSend(m.socket,AEvent.PLAYER_QUIT_TEAM .._EN_.. k .._1_.. user.name .._3_.. user.id);
					o = o + 1;
				end
				m.teamList[k] = nil;
			end
		end

		--如果是需要广播大厅的
		argTeam[k] = nil;
	end,

	--获取大厅列表
	[AEvent.GET_LOBBY_LIST] = function(socket,dataEx)
		local list = "";
		local teamListCount = 0;
		for k,v in pairs(argTeam) do
			if string.find(v.name,dataEx) == 1 then
				list = list ..v.name .._3_.. v.info .._2_;
				teamListCount = teamListCount +1;
			end
		end
		CNetSend(socket,AEvent.SEND_LOBBY_LIST .._EN_.. dataEx .._1_.. teamListCount .._1_.. list);
	end,


	-- 全局广播
	[AEvent.BROADCAST] = function(socket,dataEx)
		if not argSocket[socket] then
			return 1;
		end
		local user = argSocket[socket];
		if argUserLock[user.name] == nil then
			if (os.time()-user.lastChat)<6 then
				CNetSend(socket,AEvent.ERROR_CHAT_TIME);
			else
				--遍历argSocket,并分发
				for k,v in pairs(argSocket) do
                    if user.platform ~= nil then
					CNetSend(v.socket,AEvent.BROADCAST_C .._EN_.. user.name .._3_.. user.id .._3_.. user.vip .._3_.. user.gender .. _3_ .. user.platform .._1_.. dataEx);
                    else
					CNetSend(v.socket,AEvent.BROADCAST_C .._EN_.. user.name .._3_.. user.id .._3_.. user.vip .._3_.. user.gender .._1_.. dataEx);
					end
				end
				argSocket[socket].lastChat = os.time();
			end
		else
			CNetSend(socket,AEvent.ERROR_USER_LOCK);
		end
	end,

	-- 全局广播(除了发送人)
	[AEvent.BROADCAST_BUT_THIS] = function(socket,dataEx)
		if not argSocket[socket] then
			return 1;
		end
		local user = argSocket[socket];
		if argUserLock[user.name] == nil then
			if (os.time()-user.lastChat)<6 then
				CNetSend(socket,AEvent.ERROR_CHAT_TIME);
			else
				--遍历argSocket,并分发,如果是同一个人，则不分发
				for k,v in pairs(argSocket) do
					if v.socket ~= socket then
                     	if user.platform ~= nil then
						CNetSend(v.socket,AEvent.BROADCAST_C .._EN_.. user.name .._3_.. user.id .._3_.. user.vip .._3_.. user.gender .. _3_ .. user.platform .. _1_.. dataEx);
						else
						CNetSend(v.socket,AEvent.BROADCAST_C .._EN_.. user.name .._3_.. user.id .._3_.. user.vip .._3_.. user.gender  .. _1_.. dataEx);
						end
				   end
				end
			end
			argSocket[socket].lastChat = os.time();
		else
			CNetSend(socket,AEvent.ERROR_USER_LOCK);
		end
	end,

	--广播到群组
	[AEvent.BROADCAST_TO_TEAM] = function(socket,dataEx)
		if not argSocket[socket] then
			return 1;
		end

		local arg = dataEx:split(_1_);

--~ 		table.foreach(argSocket[socket].teamList,print);

		--如果没加入这个群组
		--print(arg[1]);
		if(arg[1]==nil or arg[2] == nil) then
			io.write('---------------------------------------nil\n');
			return 1;
		end

		if not argSocket[socket].teamList[arg[1]] then
			io.write("--------------------------------------- error:" .. socket .. ',' .. dataEx .. '\n');
			return 1;
		end

		local user = argSocket[socket];
		local userList = argTeam[arg[1]].userList;
		--遍历argSocket,并分发
		for k,v in pairs(userList) do
		    if user.platform ~= nil then
			CNetSend(v.socket,AEvent.BROADCAST_TO_TEAM_C .._EN_.. user.name.._3_.. user.id .._3_.. user.vip .._3_.. user.gender  .. _3_ .. user.platform .. _1_ .. arg[1] .._1_.. arg[2]);
			else
			CNetSend(v.socket,AEvent.BROADCAST_TO_TEAM_C .._EN_.. user.name.._3_.. user.id .._3_.. user.vip .._3_.. user.gender  .. _1_ .. arg[1] .._1_.. arg[2]);
			end
		end
	end,

	--广播到群组(除了发送人)
	[AEvent.BROADCAST_TO_TEAM_BUT_THIS] = function(socket,dataEx)
		if not argSocket[socket] then
			return 1;
		end
		local arg = dataEx:split(_1_);
		if(arg[1]==nil or arg[2] == nil) then
			io.write('---------------------------------------nil\n');
			return 1;
		end

		--如果没加入这个群组
		if not argSocket[socket].teamList[arg[1]] then
			return 1;
		end

		local user = argSocket[socket];
		local userList = argTeam[arg[1]].userList;
		--遍历argSocket,并分发
		for k,v in pairs(userList) do
			if v.socket ~= socket then
				CNetSend(v.socket,AEvent.BROADCAST_TO_TEAM_C .._EN_.. user.name .._3_.. user.id .._1_.. arg[1] .._1_.. arg[2]);
			end
		end
	end,

	--广播到队伍(除了发送人)
	[AEvent.BROADCAST_TO_FU_BUT_THIS] = function(socket,dataEx)
		if not argSocket[socket] then
			return 1;
		end
		local arg = dataEx:split(_1_);

		if(arg[1]==nil or arg[2] == nil) then
			io.write('---------------------------------------nil\n');
			return 1;
		end

		--如果没加入这个群组
		if not argSocket[socket].teamList[arg[1]] then
			return 1;
		end

		local user = argSocket[socket];
		local userList = argTeam[arg[1]].userList;
		--遍历argSocket,并分发
		for k,v in pairs(userList) do
			if v.socket ~= socket then
				CNetSend(v.socket,AEvent.BROADCAST_TO_FU_C .._EN_.. user.name .._3_.. user.id .._1_.. arg[1] .._1_.. arg[2]);
			end
		end
	end,

	--发送给某人(抄送发送人)
	[AEvent.SEND_TO_USER] = function(socket,dataEx)
		if not argSocket[socket] then
			return 1;
		end
		local arg = dataEx:split(_1_);
		if(arg[1]==nil or arg[2] == nil) then
			io.write('---------------------------------------nil\n');
			return 1;
		end

		local user = argSocket[socket];
		if argUser[arg[1]] == nil then
			CNetSend(socket,AEvent.NOTEXIST_CHAT_OBJECT);
		else
			local sendUser = argUser[arg[1]];
			CNetSend(sendUser.socket,AEvent.SEND_TO_USER_C .._EN_.. user.name .._3_.. user.id .._3_.. user.vip .._3_.. user.gender .._1_.. arg[2]);
			CNetSend(socket,AEvent.SEND_TO_USER_OWN_C .._EN_.. sendUser.name .._3_.. sendUser.id .._3_.. sendUser.vip .._3_.. sendUser.gender .._1_.. arg[2]);
		end
	end,
	--发送给某个socket
--~ 	[AEvent.SEND_TO_SOCKET] = function(socket,dataEx)
--~ 		local arg = dataEx:split(_1_);
--~ 		if(arg[1]==nil or arg[2] == nil) then
--~ 			io.write('---------------------------------------nil\n');
--~ 			return 1;
--~ 		end
--~ 		CNetSend(arg[1],arg[2]);
--~ 	end,
	--发送给某人(不抄送发送人)
	[AEvent.SEND_TO_USER_BUT_THIS] = function(socket,dataEx)
		if not argSocket[socket] then
			return 1;
		end
		local arg = dataEx:split(_1_);

		if(arg[1]==nil or arg[2] == nil) then
			io.write('---------------------------------------nil\n');
			return 1;
		end

		local user = argSocket[socket];
		if argUser[arg[1]] == nil then
			CNetSend(socket,AEvent.NOTEXIST_CHAT_OBJECT);
		else
			local sendUser = argUser[arg[1]];
			CNetSend(sendUser.socket,AEvent.SEND_TO_USER_C .._EN_.. user.name .._3_.. user.id .._3_.. user.vip .._3_.. user.gender .._1_.. arg[2]);
		end
	end,
	--获取某人信息
	[AEvent.GET_USER_INFO] = function(socket,dataEx)
		if not argSocket[socket] then
			return 1;
		end

		local arg = dataEx:split(_1_);

		local user = argUser[arg[1]];

		if user == nil then
			CNetSend(socket,AEvent.NOTEXIST_CHAT_OBJECT);
		else
			CNetSend(socket,AEvent.GET_USER_INFO_C .._EN_.. user.name .._3_.. user.id);
		end
	end,
	--获取群组列表(包含发送人)
	[AEvent.GET_TEAM_LIST] = function(socket,dataEx)
		if not argSocket[socket] then
			return 1;
		end

		local arg = dataEx:split(_1_);
		if(arg[1]==nil) then
			io.write('---------------------------------------nil\n');
			return 1;
		end
		if argTeam[arg[1]] == nil then
			io.write('---------------------------------------nil\n');
			return 1;
		end
		local userList = argTeam[arg[1]].userList;

		local userListCount = 0;
		local list = "";
		--遍历用户列表，拼成字符串发送
		for k,v in pairs(userList) do
			if(userListCount > 25) then
				break;
			else
                if v.platform ~= nil then
					list = list .. v.name .._3_.. v.id .._3_.. v.level  .._3_.. v.vip .._3_.. v.userMod .._3_.. v.leagueName .._3_.. v.title .._3_.. v.honesty .. _3_ .. v.platform .. _2_;
                else
					list = list .. v.name .._3_.. v.id .._3_.. v.level  .._3_.. v.vip .._3_.. v.userMod .._3_.. v.leagueName .._3_.. v.title .._3_.. v.honesty  .. _2_;
				end
				userListCount = userListCount + 1;
			end
		end
		CNetSend(socket,AEvent.GET_TEAM_LIST_OWN_C .._EN_.. arg[1] .._1_.. userListCount .._1_ ..list);
	end,
	--获取群组列表(不包含发送人)
	[AEvent.GET_TEAM_LIST_BUT_THIS] = function(socket,dataEx)
		if not argSocket[socket] then
			return 1;
		end

		local arg = dataEx:split(_1_);
		if(arg[1]==nil) then
			io.write('---------------------------------------nil1\n');
			return 1;
		end

		if argTeam[arg[1]] == nil then
			io.write('---------------------------------------nil2\n');
			return 1;
		end
		local userList = argTeam[arg[1]].userList;

		local userListCount = 0;
		local list = "";
		--遍历用户列表，拼成字符串发送
		for k,v in pairs(userList) do
			if(userListCount > 25) then
				break;
			else
				if v.socket ~= socket then
				    if v.platform ~= nil then
						list = list .. v.name .._3_.. v.id .._3_.. v.level  .._3_.. v.vip .._3_.. v.userMod .._3_.. v.leagueName .._3_..  v.title .._3_.. v.honesty .. _3_ .. v.platform .. _2_;
					else
						list = list .. v.name .._3_.. v.id .._3_.. v.level  .._3_.. v.vip .._3_.. v.userMod .._3_.. v.leagueName .._3_..  v.title .._3_.. v.honesty  .. _2_;
					end
					userListCount = userListCount + 1;
				end
			end
		end
		CNetSend(socket,AEvent.GET_TEAM_LIST_OWN_C .._EN_.. arg[1] .._1_.. userListCount .._1_ ..list);
	end,

	-- 系统消息到全局
	[AEvent.SYS_BROADCAST] = function(socket,dataEx)
		local arg = dataEx:split(_1_);
		if(arg[1]==nil or arg[2] == nil or arg[3] == nil) then
			io.write('---------------------------------------nil\n');
			return 1;
		end
		--判断密钥是否正确
		if systemKey ~= arg[1] then
			--输出错误
			if __DEBUG__ then
				io.write('---------------------------------------systemkey error\n');
			end
			return 1;
		end
		--遍历argSocket,并分发
		for k,v in pairs(argSocket) do
			CNetSend(v.socket,AEvent.SEND_SYSTEM_MESS_C .._EN_.. arg[2].._1_.. arg[3]);
		end
	end,
	-- 系统消息到个人
	[AEvent.SYS_SEND_USER] = function(socket,dataEx)
		local arg = dataEx:split(_1_);
		if(arg[1]==nil or arg[2] == nil or arg[3] == nil or arg[4] ==nil) then
			return 1;
		end
		--判断密钥是否正确
		if systemKey ~= arg[1] then
			--输出错误
			if __DEBUG__ then
				io.write('---------------------------------------systemkey error\n');
			end
			return 1;
		end
		if argUser[arg[2]] == nil then
--~ 			CNetSend(socket,AEvent.NOTEXIST_CHAT_OBJECT);
		else
			local sendUser = argUser[arg[2]];
			CNetSend(sendUser.socket,AEvent.SEND_SYSTEM_MESS_C .._EN_.. arg[3].._1_.. arg[4]);
		end
	end,
	-- 系统消息到群组
	[AEvent.SYS_SEND_TEAM] = function(socket,dataEx)
		local arg = dataEx:split(_1_);
		if(arg[1]==nil or arg[2] == nil or arg[3] == nil or arg[4] ==nil) then
			return 1;
		end
		--判断密钥是否正确
		if systemKey ~= arg[1] then
			--输出错误
			if __DEBUG__ then
				io.write('---------------------------------------systemkey error\n');
			end
			return 1;
		end
		if argTeam[arg[2]] ~= nil then
			local userList = argTeam[arg[2]].userList;

			--遍历argSocket,并分发
			for k,v in pairs(userList) do
				CNetSend(v.socket,AEvent.SEND_SYSTEM_MESS_C .._EN_.. arg[3].._1_.. arg[4]);
			end
		end
	end,
	-- 获取在线人数
	[AEvent.GET_USER_COUNT] = function(socket,dataEx)
		local userCount = 1;
		for k,v in pairs(argSocket) do
			userCount = userCount + 1;
		end
		CNetSend(socket,AEvent.SEND_USER_COUNT .._EN_.. userCount);
	end,
	-- 获取大地图列表
	[AEvent.GET_USER_MAP] = function(socket,dataEx)
		local list = "";
		local userListCount = 0;
		for k,v in pairs(argSocket) do
			if(userListCount > 10) then
				break;
			else
				if v.socket ~= socket then
					 if v.platform ~= nil then
						list = list .. v.name .._3_.. v.id .._3_.. v.level  .._3_.. v.vip .._3_.. v.userMod .._3_.. v.leagueName .._3_..  v.title .._3_.. v.honesty .. _3_ .. v.platform .. _2_;
					else
					    list = list .. v.name .._3_.. v.id .._3_.. v.level  .._3_.. v.vip .._3_.. v.userMod .._3_.. v.leagueName .._3_..  v.title .._3_.. v.honesty  .. _2_;
					end
				userListCount = userListCount + 1;
				end
			end
		end
		CNetSend(socket,AEvent.SEND_USER_MAP .._EN_.. list);
	end,
	-- 修改角色信息
	[AEvent.UPDATE_USER_STATE] = function(socket,dataEx)
		local arg = dataEx:split(_1_);
		if(arg[1]==nil or arg[2] == nil or arg[3] == nil or arg[4] ==nil) then
			return 1;
		end
		--判断密钥是否正确
		if systemKey ~= arg[1] then
			--输出错误
			if __DEBUG__ then
				io.write('---------------------------------------systemkey error\n');
			end
			return 1;
		end
		if arg[3] == 1 then
			argUser[arg[2]].level = arg[4];
		end
		if arg[3] == 2 then
			argUser[arg[2]].vip = arg[4];
		end
		if arg[3] == 3 then
			argUser[arg[2]].honesty = arg[4];
		end
		if arg[3] == 4 then
			argUser[arg[2]].lock = arg[4];
			--加入黑名单
			if not argUserLock[arg[2]] then
				argUserLock[arg[2]] = arg[2];
			end
		end
		if arg[3] == 5 then
			argUser[arg[2]].userMod = arg[4];
		end

	end,
	-- 加入黑名单
	[AEvent.KILL_USER] = function(socket,dataEx)
		local arg = dataEx:split(_1_);

		if (arg[1] == nil or arg[2] == nil) then
			return 1;
		end
		--判断密钥是否正确
		if systemKey ~= arg[1] then
			--输出错误
			if __DEBUG__ then
				io.write('---------------------------------------systemkey error\n');
			end
			return 1;
		end


		if argUser[arg[2]] == nil then
			CNetSend(socket,AEvent.NOTEXIST_CHAT_OBJECT);
		else
--~ 			OnClose_Socket(argUser[arg[2]].socket);
		end

		--加入黑名单
		if not argUserLock[arg[2]] then
			argUserLock[arg[2]] = arg[2];
		end
	end,
	-- 从黑名单中删除
	[AEvent.FREE_USER] = function(socket,dataEx)
		local arg = dataEx:split(_1_);
		--判断密钥是否正确
		if systemKey ~= arg[1] then
			--输出错误
			if __DEBUG__ then
				io.write('---------------------------------------systemkey error\n');
			end
			return 1;
		end

		if argUser[arg[2]] == nil then
			CNetSend(socket,AEvent.NOTEXIST_CHAT_OBJECT);
		else
			--从黑名单中删除
			if argUserLock[arg[2]] then
				argUserLock[arg[2]] = nil;
				io.write('--------------------------------------- free user ok\n');
			end
		end
	end,
}

